package mxnet;
import org.w3c.dom.css.Rect;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import javax.imageio.ImageIO;

public class ImageTest {

    public static class Rect{
        public String className;
        public float probability;
        public int x;
        public int y;
        public int w;
        public int h;

        public Rect(String className, float probability, int x, int y, int w, int h) {
            this.className = className;
            this.probability = probability;
            this.x = x;
            this.y = y;
            this.w = w;
            this.h = h;
        }
    }



    public static void main(String[] args) throws IOException {
        String inName= "D:\\mxnet\\java-demo\\tmp\\inputImages\\resnetssd\\dog-ssd.jpg";
        String outName= "D:\\mxnet\\java-demo\\tmp\\inputImages\\resnetssd\\dog-ssd2.jpg";
        Rect r1 = new Rect("狗dog",0.92f,100,100,120,140);
        java.util.List<Rect> list = new ArrayList<Rect>();
        list.add(r1);

        draw(inName, outName, list);
    }

    public static void draw(String inName, String outName, java.util.List<Rect> list)   {
        BufferedImage image = null;
        try {
            image = ImageIO.read(new File(inName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        java.util.List<Color> clist = new ArrayList<Color>();
        clist.add(Color.RED);
        clist.add(Color.BLUE);
        clist.add(Color.GREEN);
        clist.add(Color.ORANGE);
        clist.add(Color.CYAN);

        Graphics2D g = (Graphics2D) image.getGraphics();
        int size =18;
        int skip = 2;
        g.setStroke(new BasicStroke(3.0f));
        g.setFont(new Font("Tahoma", Font.BOLD, size));
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING , RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        for(Rect r: list){
            int ci =(int)(Math.random()*clist.size()) ;
            Color rc = clist.get(ci);
            g.setColor(rc);//画笔颜色
            g.drawRect(r.x, r.y, r.w-r.x, r.h-r.y);//矩形框(原点x坐标，原点y坐标，矩形的长，矩形的宽)
            g.drawString(r.className, r.x+skip, r.y+size+skip);
            g.drawString(r.probability+"", r.x+skip, r.y+size*2+skip*2);
        }
        g.dispose();
        //输出图片的地址
        try {
            FileOutputStream  out = new FileOutputStream(outName);
            ImageIO.write(image, "jpeg", out);
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}